#define _CRT_SECURE_NO_WARNINGS
#include"stack.h"

void StackInit(stack* sp)
{
	assert(sp);
	sp->date = (Stdatetype*)malloc(sizeof(Stdatetype) * 4);
	if (sp->date == NULL)
	{
		perror("malloc");
		return;
	}
	sp->top = 0;
	sp->capacity = 4;
}

void StackDestroy(stack* sp)
{
	assert(sp);
	free(sp->date);
}

void StackPush(stack* sp, Stdatetype x)
{
	assert(sp);
	if (sp->top == sp->capacity)
	{
		Stdatetype* tmp = (Stdatetype*)realloc(sp->date, sizeof(Stdatetype) * sp->capacity * 2);
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		sp->capacity = sp->capacity * 2;
	}
	sp->date[sp->top] = x;
	sp->top++;
}

void StackPop(stack* sp)
{
	assert(sp);
	assert(!StackEmpty(sp));
	sp->top--;
}

int StackEmpty(stack* sp)
{
	assert(sp);
	if (sp->top == 0)return 1;
	else return 0;
}

int StackSize(stack* sp)
{
	assert(sp);
	return sp->top;
}

Stdatetype StackTop(stack* sp)
{
	assert(sp);
	assert(!StackEmpty(sp));
	return sp->date[sp->top - 1];
}